package com.freesun.shop.controller;

import com.freesun.shop.entity.Area;
import com.freesun.shop.service.AreaService;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/area")
public class AreaController {

    @Reference(check = false)
    private AreaService areaService;

    @ApiOperation("查询所有的区域")
    @GetMapping("/list")
    public ResponseEntity<List<Area>> getAreaList() {
        List<Area> areaList = areaService.list();
        return ResponseEntity.ok(areaList);
    }

    @ApiOperation("删除一个区域")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        areaService.removeById(id);
        return ResponseEntity.ok().build();
    }
    @ApiOperation("新增一个区域")
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody Area area){
        areaService.save(area);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("根据ID查询区域")
    @GetMapping("/info/{id}")
    public ResponseEntity<Area> findById(@PathVariable Long id) {
        Area area = areaService.getById(id);
        return ResponseEntity.ok(area);
    }

    @ApiOperation("修改区域的信息")
    @PutMapping()
    public ResponseEntity<Void> update(@RequestBody Area area) {
        areaService.updateById(area);
        return ResponseEntity.ok().build();
    }
}
